var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);

app.get("/", function (req, res) {
  res.sendFile(__dirname + "/index.html");
});

let TYPE_INTO = 0;
let TYPE_LEAVE = 1;
let TYPE_MSG = 2;

io.on("connection", function (socket) {
  socket.on("into", function (name) {
    io.sockets.emit("into", { type: TYPE_INTO, msg: `${name}进入聊天室` });
  });

  // socket.on("disconnect", function (name) {
  //   socket.emit("leave", {
  //     type: TYPE_LEAVE,
  //     msg: `${name}离开聊天室`,
  //   });
  // });

  socket.on("msg", function (msg) {
    io.sockets.emit("msg", msg);
  });
});

http.listen(3000, function () {
  console.log("listening on *:3000");
});
